package com.ruoyi.mateThrough.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateRequistion.domain.MateRequistion;
import com.ruoyi.mateRequistion.mapper.MateRequistionMapper;
import com.ruoyi.mateThrough.domain.MateThrough;
import com.ruoyi.mateThrough.mapper.MateThroughMapper;
import com.ruoyi.mateThrough.service.MateThroughService;
import com.ruoyi.mtAudit.domain.MtAudit;
import com.ruoyi.mtAudit.mapper.MtAuditMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@Service
public class MateThroughServiceImpl implements MateThroughService {
    @Autowired
    MateThroughMapper mateThroughMapper;

    @Autowired
    MtAuditMapper mtAuditMapper;

    @Autowired
    MateRequistionMapper mateRequistionMapper;

    @Override
    public int addThrough(MateThrough mateThrough) {
        int num = 0;
        try {
            num = mateThroughMapper.addThrough(mateThrough);
            MtAudit mtAudit = new MtAudit();
            mtAudit.setCompanyId(SecurityUtils.getDeptId());
            mtAudit.setOrderId(mateThrough.getThroughId());
            mtAudit.setZjName(mateThrough.getAuditName());
            mtAudit.setOrderType("6");
            mtAudit.setOrderNum(mateThrough.getThroughNum());
            mtAudit.setAuditStatus("0");
            mtAudit.setAuditBy(SecurityUtils.getLoginUser().getNickName());
            mtAuditMapper.insertMtAudit(mtAudit);


        } catch (Exception e) {
            mateThroughMapper.removeMateThrough(mateThrough.getThroughId());
            Map<String, Object> resMap = new HashMap<>();
            resMap.put("orderId", mateThrough.getThroughId());
            resMap.put("orderType", "6");
            mtAuditMapper.deleteMtAuditById(resMap);
        }

        return num;
    }

    @Override
    public List<MateThrough> selectThrough(MateThrough mateThrough) {
        return mateThroughMapper.selectThrough(mateThrough);
    }

    @Override
    public MateThrough selectMateThrough(Long throughId) {
        return mateThroughMapper.selectMateThrough(throughId);
    }

    @Override
    public int updatemateThrough(MateThrough mateThrough) {
        return mateThroughMapper.updatemateThrough(mateThrough);
    }

    @Override
    public void remove(Long throughId) {
        mateThroughMapper.removeMateThrough(throughId);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("orderId", throughId);
        resMap.put("orderType", "6");
        mtAuditMapper.deleteMtAuditById(resMap);

    }
}
